package com.gzx.plugin.tjzy.modular.inspection.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gzx.plugin.tjzy.modular.inspection.bo.GzxManualInspectionAddBo;
import com.gzx.plugin.tjzy.modular.inspection.bo.GzxManualInspectionQueryBo;
import com.gzx.plugin.tjzy.modular.inspection.constant.InspectionConstant;
import com.gzx.plugin.tjzy.modular.inspection.entity.GzxManualInspection;
import com.gzx.plugin.tjzy.modular.inspection.mapper.GzxManualInspectionMapper;
import com.gzx.plugin.tjzy.modular.inspection.service.GzxManualInspectionService;
import com.gzx.plugin.tjzy.modular.inspection.vo.GzxManualInspectionVo;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.mvc.service.impl.BaseServiceImpl;
import vip.xiaonuo.common.util.CommonCryptogramUtil;
import vip.xiaonuo.common.util.CommonExcelUtil;

import java.util.List;

/**
 * @author xbl
 */
@Slf4j
@Service
@DS("tjzy-async")
public class GzxManualInspectionServiceImpl extends BaseServiceImpl<GzxManualInspectionAddBo, GzxManualInspectionVo, GzxManualInspectionQueryBo, GzxManualInspectionMapper, GzxManualInspection> implements GzxManualInspectionService {

    public GzxManualInspectionServiceImpl(GzxManualInspectionMapper baseMapper) {
        super(baseMapper);
    }

    /**
     * 人工质检分页
     */
    @Override
    protected LambdaQueryWrapper<GzxManualInspection> buildQueryWrapper(GzxManualInspectionQueryBo queryBo) {
        log.info("人工质检分页-查询参数：{} ", queryBo);
        return Wrappers.<GzxManualInspection>lambdaQuery()
                .eq(StringUtils.isNotBlank(queryBo.getCallId()), GzxManualInspection::getCallId, queryBo.getCallId())
                .eq(StringUtils.isNotBlank(queryBo.getCallee()), GzxManualInspection::getCallee, CommonCryptogramUtil.doSm1AesEncrypt(queryBo.getCallee()))
                .eq(StringUtils.isNotBlank(queryBo.getCaller()), GzxManualInspection::getCaller, queryBo.getCaller())
                .eq(StringUtils.isNotBlank(queryBo.getQcInspector()), GzxManualInspection::getQcInspector, queryBo.getQcInspector())
                .eq(StringUtils.isNotBlank(queryBo.getAgentNum()), GzxManualInspection::getAgentNum, queryBo.getAgentNum())
                .eq(StringUtils.isNotBlank(queryBo.getUserName()), GzxManualInspection::getUserName, queryBo.getUserName())
                .eq(StringUtils.isNotBlank(queryBo.getDeptId()), GzxManualInspection::getDeptId, queryBo.getDeptId())
                .eq(StringUtils.isNotBlank(queryBo.getInspection()), GzxManualInspection::getInspection, queryBo.getInspection())
                .eq(StringUtils.isNotBlank(queryBo.getRemark()), GzxManualInspection::getRemark, queryBo.getRemark())
                .between(StringUtils.isNotBlank(queryBo.getBeginTime()) && StringUtils.isNotBlank(queryBo.getEndTime()), GzxManualInspection::getCreateTime, queryBo.getBeginTime(), queryBo.getEndTime())
                .orderByDesc(GzxManualInspection::getId);
    }

    /**
     * 人工质检导出
     */
    @Override
    public void exportInspectionReport(GzxManualInspectionQueryBo queryBo, HttpServletResponse response) {
        if (StringUtils.isEmpty(queryBo.getBeginTime()) || StringUtils.isEmpty(queryBo.getEndTime())) {
            throw new CommonException("时间不允许为空");
        }
        //只导出查询的数据
        List<GzxManualInspectionVo> inspectionVo = baseMapper.selectVoList(Wrappers.<GzxManualInspection>lambdaQuery()
                .eq(StringUtils.isNotBlank(queryBo.getCallId()), GzxManualInspection::getCallId, queryBo.getCallId())
                .eq(StringUtils.isNotBlank(queryBo.getCallee()), GzxManualInspection::getCallee, CommonCryptogramUtil.doSm1AesEncrypt(queryBo.getCallee()))
                .eq(StringUtils.isNotBlank(queryBo.getQcInspector()), GzxManualInspection::getQcInspector, queryBo.getQcInspector())
                .eq(StringUtils.isNotBlank(queryBo.getCaller()), GzxManualInspection::getCaller, queryBo.getCaller())
                .eq(StringUtils.isNotBlank(queryBo.getAgentNum()), GzxManualInspection::getAgentNum, queryBo.getAgentNum())
                .eq(StringUtils.isNotBlank(queryBo.getUserName()), GzxManualInspection::getUserName, queryBo.getUserName())
                .eq(StringUtils.isNotBlank(queryBo.getDeptId()), GzxManualInspection::getDeptId, queryBo.getDeptId())
                .eq(StringUtils.isNotBlank(queryBo.getInspection()), GzxManualInspection::getInspection, queryBo.getInspection())
                .eq(StringUtils.isNotBlank(queryBo.getRemark()), GzxManualInspection::getRemark, queryBo.getRemark())
                .between(StringUtils.isNotBlank(queryBo.getBeginTime()) && StringUtils.isNotBlank(queryBo.getEndTime()), GzxManualInspection::getCreateTime, queryBo.getBeginTime(), queryBo.getEndTime())
                .orderByDesc(GzxManualInspection::getId));
        if (CollUtil.isNotEmpty(inspectionVo)) {
            //封装数据到excel中
            setInspectionReport(inspectionVo);
            //导出
            CommonExcelUtil.exportExcel("人工质检", inspectionVo, GzxManualInspectionVo.class, response);
        } else {
            throw new CommonException("无可导出的数据");
        }
    }

    /**
     * 封装数据到excel中
     */
    private void setInspectionReport(List<GzxManualInspectionVo> inspectionVo) {
        for (GzxManualInspectionVo vo : inspectionVo) {
            //导出的手机号要解密
            vo.setCallee(CommonCryptogramUtil.doSm1AesDecrypt(vo.getCallee()));
            //质检结果
            InspectionConstant result = InspectionConstant.fromCode(Integer.parseInt(vo.getInspection()));
            vo.setInspection(result.getDescription());
            //导出的部门名称没有自动翻译出来 FIXME：导出的时候没有自动翻译，以及获取字典报数据源的问题
        }
    }
}
